package tq.com.tqteacher.mine.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.systools.system.StringHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;

public class TQInputActivity extends BaseActivity {
    private TQHeader5View mHeader5View;
    private TextView mTitle;
    private EditText mInput;

    static private String title;
    static private  String placeHold;
    static private  int max;

    static public String RESULT_CONTENT = "RESULT_CONTENT";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStatusBar();
        setContentView(R.layout.activity_input_layout);

        mHeader5View = findViewById(R.id.title);
        mTitle = findViewById(R.id.title1);
        mInput = findViewById(R.id.input);

        mHeader5View.getTitleView().setText(title);
        mHeader5View.getRightView().setText("完成");
        mHeader5View.getRightView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commit();
            }
        });
        mHeader5View.getBackIcon().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQInputActivity.this.finish();
            }
        });

        mTitle.setText(String.format("%s:", title));
        mInput.setHint(placeHold);

        mInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void commit(){
        String string = StringHelper.editText(mInput);
        if(StringHelper.isEmpty(string)){
            AlertDialogHelper.show(this, "输入内容不能为空");
            return;
        }

        Intent lIntent = new Intent();
        lIntent.putExtra(RESULT_CONTENT, string);
        setResult(Activity.RESULT_OK, lIntent);
        this.finish();
    }
    public static void show(Activity pActivity, int requestCode, String title, String placehold, int max){
        TQInputActivity.title = title;
        TQInputActivity.placeHold = placehold;
        TQInputActivity.max = max;

        Intent lIntent = new Intent(pActivity, TQInputActivity.class);
        pActivity.startActivityForResult(lIntent, requestCode);
    }

    public static void show(Fragment pFragment, int requestCode, String title, String placehold, int max){
        TQInputActivity.title = title;
        TQInputActivity.placeHold = placehold;
        TQInputActivity.max = max;

        Intent lIntent = new Intent(pFragment.getContext(), TQInputActivity.class);
        pFragment.startActivityForResult(lIntent, requestCode);
    }
}
